<script>
import basepath from '../../plugins/base.js'
export default {
  data() {
    return {
      baseimg: '',
      baseapi: '',

      types: [],
      specs: [],

      // form: {
      //   tid: 1,
      //   images: '',
      //   name: '',
      //   content: '',
      //   price: 0,
      //   unit: '杯',
      //   use_property: true,
      //   property: [
      //     {
      //       gskid: 1,  //规格 主键表  GoodSpecKey  id
      //       values: [],
      //       checked: true
      //     },
      //     {
      //       gskid: 2,
      //       values: [],
      //       checked: true
      //     },
      //     {
      //       gskid: 3,
      //       values: [],
      //       checked: true
      //     }
      //   ]
      // }
    }
  },
  props: ['form'],
  methods: {
    //上传成功
    handleAvatarSuccess(res, file) {
      // console.log('file: ', file);
      // console.log('res: ', res);
      if (res.code == 1) {
        this.$message.success('图片上传成功!');
        this.form.images = res.filepath;
      } else {
        this.$message.error(res.msg);
      }
    },
    //上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';

      const isLtMB = file.size / 1024 / 1024 < 10;

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 和 PNG 格式!');
      }
      if (!isLtMB) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return (isJPG || isPNG) && isLtMB;
    },

    onSubmit() {

    }
  },
  created() {
    this.baseimg = basepath.imgurl;
    this.baseapi = basepath.apiurl;

    this.axios({
      method: 'get',
      url: 'Spec'
    }).then(response => {
      this.specs = response;
    });

    this.axios({
      method: 'get',
      url: 'GoodType'
    }).then(response => {
      //console.log('response: ', response);
      this.types = response;
    })

  },
}
</script>

<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px"
      style="overflow: hidden;">

      <el-col :span="6" :offset="1">
        <el-form-item label="商品类型">
          <el-select v-model="form.tid" placeholder="请选择类型">
            <el-option v-for="item in types" :key="item.id" :label="item.name"
              :value="item.id"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload class="avatar-uploader" :action="baseapi+'/Files'"
            :show-file-list="false" :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.images" :src="baseimg+form.images" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="商品单位">
          <el-input v-model="form.unit"></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="14" :offset="2">
        <el-form-item label="属性">
          <el-switch v-model="form.use_property" active-color="#13ce66"
            inactive-color="#ff4949"></el-switch>
        </el-form-item>

        <!-- 循环规格和规格的值 -->
        <el-form-item v-for="(item,index) in specs" :key="item.id"
          :label="item.name" v-show="form.use_property">
          <el-switch v-model="form.property[index].checked"
            active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          <el-checkbox-group v-show="form.property[index].checked"
            v-model="form.property[index].values">
            <el-checkbox v-for="v in item.SpecValue" :name="item.name" :key=v.id
              :label="v.value" :value="v.id"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

      </el-col>
    </el-form>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>